package com.zhaopin.thread;

import com.alibaba.fastjson.JSONObject;
import com.zhaopin.utils.DateUtils;
import org.apache.commons.io.FileUtils;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.PrintWriter;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.concurrent.Callable;

/**
 * @author Administrator
 * @create 2019-05-21 16:10
 */
public class CompanyThread implements Runnable {

    private String filePath;

    public String getFilePath() {
        return filePath;
    }

    public void setFilePath(String filePath) {
        this.filePath = filePath;
    }

    public CompanyThread(String filePath) {
        this.filePath = filePath;
    }

    @Override
    public void run() {
        System.out.println(Thread.currentThread().getName() + " " + filePath);

        //按行读取文件
        try {
            List<String> list = FileUtils.readLines(new File(filePath), "utf-8");

            // 此处进行处理数据，写入到一个文件中去
            PrintWriter pw = new PrintWriter(new File(filePath + ".json"), "utf-8");
            int i = 0;
            for (String s : list) {
                i++;
                //如果第一行为标头 则第一行不做处理
                // {"index":{"_id":"58337642"}}
                //进行数据的处理 "58337642,CC583376428,2015-11-01T07:11:20.000Z,河北佛教文化发展有限公司"  暂时先认为是这样的数据
                String[] attr = s.split(",");

                JSONObject jsonIndex = new JSONObject();
                jsonIndex.put("_id", attr[0]);
                JSONObject jsonIndexOut = new JSONObject();
                jsonIndexOut.put("index", jsonIndex);

                JSONObject jsonObject = new JSONObject();
                jsonObject.put("number", attr[0]);
                jsonObject.put("id", attr[1]);
                if (!"\\N".equals(attr[2])) {
                    jsonObject.put("createTime", getFormatedTime(attr[2]));
                }
                if(attr.length>3){
                    if(null == attr[3] || "".equals(attr[3])){
                        continue;
                    }
                    jsonObject.put("name", attr[3]);
                }else{
                    continue;
                }

                //不写入elasticsearch的话则该项目进行
                pw.println(jsonIndexOut.toJSONString());
                pw.println(jsonObject.toJSONString());
                System.out.println(Thread.currentThread().getName() + ": " + i + " " + jsonObject.getString("id"));
            }
            pw.flush();
            pw.close();
        } catch (IOException | ParseException e) {
            e.printStackTrace();
        }
    }

    private String getFormatedTime(String dateInput) throws ParseException {
        String dataOutput = "";

        Date date = new SimpleDateFormat(DateUtils.yyyyMMddHHmmss).parse(dateInput);
        //获取到 time的时间戳的值
        long time = date.getTime();
        dataOutput = DateUtils.getTimestampToFormatedTime(time + "", DateUtils.yyyyMMddTHHmmss);
        dataOutput += ".000Z";
        return dataOutput;
    }

}
